var mqtt = require('mqtt/dist/mqtt.js')

let mqttTool = {
  client: null
}

mqttTool.connect = function (params) {
  let options = {
    port: 443,
    host: params.url,
    protocol: 'wss',
    path: '/mqtt',
    clientId: params.clientId,
    username: params.username,
    password: params.password,
    connectTimeout: 5000,
    clean: false,
    rejectUnauthorized: false,
  }
  let client = mqtt.connect(options);
  // console.log("client", client);
  mqttTool.client = client
  return client;
}

mqttTool.end = function () {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接')
      console.log('App_text' + ":end 未连接")
      return;
    }
    mqttTool.client.end()
    mqttTool.client = null
    resolve('连接终止')
  })
}

mqttTool.reconnect = function () {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接')
      console.log('App_text' + ":reconnect 未连接")
      return;
    }
    mqttTool.client.reconnect()
  })
}

mqttTool.subscribe = function (params) {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接')
      console.log('App_text' + ":unconnect 未连接")
      return;
    }
    mqttTool.client.subscribe(params.topic, {
      qos: params.qos
    }, function (err, res) {
      console.log(err, res)
      if (!err && res.length > 0) {
        resolve('订阅成功')
        console.log('App_text' + ":subscribe success 订阅成功")
      } else {
        resolve('订阅失败')
        console.log('App_text' + ":subscribe failed 订阅失败")
        return;
      }
    })
  })
}

mqttTool.unsubscribe = function (params) {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接')
      console.log('App_text' + ":unconnect 未连接")
      return;
    }
    mqttTool.client.unsubscribe(params.topic, function (err) {
      if (!err) {
        resolve('取消订阅成功')
        console.log('App_text' + ":unsubscribe success 取消订阅成功")
      } else {
        resolve('取消订阅失败')
        console.log('App_text' + ":unsubscribe failed 取消订阅失败")
        return;
      }
    })
  })
}

mqttTool.publish = function (params) {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接')
      console.log('App_text' + ":unconnect 未连接")
      return;
    }
    mqttTool.client.publish(params.topic, params.message, function (err) {
      if (!err) {
        resolve(params.topic + '-' + params.message + '-发送成功')
        console.log('App_text' + ":publish success 发送成功")
      } else {
        resolve(params.topic + '-' + params.message + '-发送失败')
        console.log('App_text' + ":publish failed 发送失败")
        return;
      }
    })
  })
}

export default mqttTool